﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TireService.Data
{
    public class CustomerSubscriptionBLL
    {

        public void UpdateCustomerSubscription(TireServiceDB db, CustomerSubscription customersubscription, Customer customer, int CustomerSubscriptionId, int SubscriptionId, DateTime LastRenewalDate, DateTime NextRenewalDate, bool InActive, bool BalancingIncluded, decimal Price, Guid UserId, bool SubmitInTransaction, string LogNote)
        {
            try
            {
                // Check if existing subscription is being updated
                if (CustomerSubscriptionId != 0)
                {
                    customersubscription = db.GetCustomerSubscription(CustomerSubscriptionId);
                }

                // Initiate business logic - create log
                CustomerSubscriptionLogBLL bll = new CustomerSubscriptionLogBLL();

                customer.CustomerSubscriptions.Add(customersubscription);
                //customersubscription.CustomerId = CustomerId;
                customersubscription.SubscriptionId = SubscriptionId;
                customersubscription.LastRenewalDate = LastRenewalDate;
                customersubscription.NextRenewalDate = NextRenewalDate;
                customersubscription.InActive = InActive;
                //customersubscription.Paid = Paid; OUTDATED: 15-12-2012
                customersubscription.BalancingIncluded = BalancingIncluded;
                //customersubscription.CarHelpIncluded = CarHelpIncluded; OUTDATED: 15-12-2012
                customersubscription.Price = Price;
                    
                // Create new customer subscription
                if (customersubscription.CustomerSubscriptionId == 0)
                {
                    // Add to database
                    db.AddCustomerSubscription(customersubscription);

                    // Created customer subscription - create log
                    bll.CreateCustomerSubscriptionLog(db, customersubscription, (int)Shared.SubscriptionLogAction.Created, UserId, LogNote);
                }
                else
	            {
                    // Edited customer subscription - create log
                    bll.CreateCustomerSubscriptionLog(db, customersubscription, (int)Shared.SubscriptionLogAction.Edited, UserId, LogNote);
	            }

                // Check if customer subscription is being created/updated in other transaction
                if (!SubmitInTransaction)
                {
                    // Submit changes to database
                    db.SubmitChanges();

                    // REMARK: Outdate 27-12-2012 - subscriptio part in E-conomic outphased
                    //// Check if sync is required
                    //if (customer.SyncToEconomic)
                    //{
                    //    // Update customer subscription in E-conomic
                    //    EconomicInterface ecoInterface = new EconomicInterface();

                    //    // Insert subscription in Economic
                    //    // Check if invoice is paid by parent customer
                    //    if (customer.ParentPayInvoice & customer.ParentCustomerId != null)
                    //    {
                    //        // Create subscription for parent customer
                    //        ecoInterface.UpdateSubscriber(customersubscription.SubscriptionId, customersubscription, Convert.ToInt32(customer.ParentCustomerId), customer.Name);
                    //    }
                    //    else
                    //    {
                    //        // Create subscription for customer
                    //        ecoInterface.UpdateSubscriber(customersubscription.SubscriptionId, customersubscription, null, customer.Name);
                    //    }
                    //}
                }

            }
            catch (Exception ex)
            {
                
            }
        }

        // REMARK: Outdate 27-12-2012 - new function made for this in usercontrol (deactivate subscription)
        //public void ExpireCustomerSubscription(TireServiceDB db, CustomerSubscription customersubscription, DateTime ExpireDate, Guid UserId, bool SubmitInTransaction)
        //{
        //    try
        //    {
        //        // Initiate business logic - create log
        //        CustomerSubscriptionLogBLL bll = new CustomerSubscriptionLogBLL();

        //        customersubscription.InActiveDate = ExpireDate;
        //        customersubscription.InActive = true;

        //        // Create log
        //        bll.CreateCustomerSubscriptionLog(db, customersubscription, (int)Shared.SubscriptionLogAction.DeActivated, UserId, null);

        //        // TODO: Create code
        //        int EconomicSubscriptionNumber = 0;

        //        // Check if customer subscription is being created/updated in other transaction
        //        if (!SubmitInTransaction)
        //        {
        //            // Submit changes to database
        //            db.SubmitChanges();

        //            // Check if sync is required
        //            if (db.GetCustomerSyncToEconomic(customersubscription.CustomerId))
        //            {

        //                // TODO: Add interfacejob to queue

        //                // Update customer subscription in E-conomic
        //                EconomicInterface ecoInterface = new EconomicInterface();

        //                ecoInterface.ExpireSubscriber(EconomicSubscriptionNumber, customersubscription);

        //                // TODO: Remove interfacejob to queue
        //            }
        //        }

        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //}

        // REMARK: Outdate 27-12-2012 - subscription part in E-conomic outphased
        //public void ReActivateCustomerSubscription(TireServiceDB db, CustomerSubscription customersubscription, Guid UserId, bool SubmitInTransaction)
        //{
        //    try
        //    {
        //        // Initiate business logic - create log
        //        CustomerSubscriptionLogBLL bll = new CustomerSubscriptionLogBLL();

        //        customersubscription.InActiveDate = null;
        //        customersubscription.InActive = false;

        //        // Create log
        //        bll.CreateCustomerSubscriptionLog(db, customersubscription, (int)Shared.SubscriptionLogAction.ReActivated, UserId, null);

        //        // TODO: Create code
        //        int EconomicSubscriptionNumber = 0;

        //        // Check if customer subscription is being reactivated in other transaction
        //        if (!SubmitInTransaction)
        //        {
        //            // Submit changes to database
        //            db.SubmitChanges();

        //            // Check if sync is required
        //            if (db.GetCustomerSyncToEconomic(customersubscription.CustomerId))
        //            {
        //                // TODO: Add interfacejob to queue

        //                // Update customer subscription in E-conomic
        //                EconomicInterface ecoInterface = new EconomicInterface();

        //                ecoInterface.ReActivateSubscriber(EconomicSubscriptionNumber, customersubscription);

        //                // TODO: Remove interfacejob to queue
        //            }
        //        }

        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //}

        // REMARK: Outdate 27-12-2012 - subscription part in E-conomic outphased
        //public void ChangeSubscription(TireServiceDB db, CustomerSubscription customersubscription, Guid UserId, bool SubmitInTransaction, int NewSubscriptionId)
        //{
        //    try
        //    {
        //        // Initiate business logic - create log
        //        CustomerSubscriptionLogBLL bll = new CustomerSubscriptionLogBLL();

        //        customersubscription.SubscriptionId = NewSubscriptionId;

        //        // Create log
        //        bll.CreateCustomerSubscriptionLog(db, customersubscription, (int)Shared.SubscriptionLogAction.SubscriptionChanged, UserId, null);

        //        // TODO: Create code
        //        int EconomicSubscriptionNumber = 0;
        //        int NewEconomicSubscriptionNumber = 0;

        //        // Check if customer subscription is being reactivated in other transaction
        //        if (!SubmitInTransaction)
        //        {
        //            // Submit changes to database
        //            db.SubmitChanges();

        //            // Check if sync is required
        //            if (db.GetCustomerSyncToEconomic(customersubscription.CustomerId))
        //            {

        //                // TODO: Add interfacejob to queue

        //                // Update customer subscription in E-conomic
        //                EconomicInterface ecoInterface = new EconomicInterface();

        //                ecoInterface.ChangeSubscriberSubscription(EconomicSubscriptionNumber, NewEconomicSubscriptionNumber, customersubscription);

        //                // TODO: Remove interfacejob to queue
        //            }
        //        }

        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //}

        public string GetSubscriptionNumber(CustomerSubscription customersubscription)
        {
            return customersubscription.CustomerId.ToString() + "-" + customersubscription.SubscriptionId.ToString() + "-" + customersubscription.CustomerSubscriptionId.ToString();
        }

        public DateTime CalculateLastRenewalDate(DateTime dateinput)
        {
            DateTime SummerPeriodStart = Convert.ToDateTime("01-03-" + dateinput.Year.ToString());
            DateTime WinterPeriodStart = Convert.ToDateTime("01-09-" + dateinput.Year.ToString());

            if (dateinput < SummerPeriodStart)
            {
                dateinput = SummerPeriodStart;
            }

            if (SummerPeriodStart <= dateinput & dateinput < WinterPeriodStart)
            {
                dateinput = SummerPeriodStart;
            }

            if (WinterPeriodStart <= dateinput)
            {
                dateinput = WinterPeriodStart;
            }

            return dateinput;
        }

        public DateTime CalculateNextRenewalDate(DateTime LastRenewalDate, Subscription subscription)
        {
            DateTime NextRenewalDate = LastRenewalDate.AddMonths(subscription.SubscriptionLengthMonth); //NOTE: Only preliminary value - correct one will be calculated below
            int year = LastRenewalDate.Year;
            DateTime SummerPeriodStart = Convert.ToDateTime("01-03-" + year.ToString());
            DateTime WinterPeriodStart = Convert.ToDateTime("01-09-" + year.ToString());
            
            // 06-03-2013 LastRenewal -> 06-03-2014 NextRenewal

            if (LastRenewalDate < SummerPeriodStart)
            {
                NextRenewalDate = SummerPeriodStart;
            }

            if (SummerPeriodStart <= LastRenewalDate & LastRenewalDate < WinterPeriodStart)
            {
                NextRenewalDate = SummerPeriodStart.AddMonths(subscription.SubscriptionLengthMonth);
            }

            if (WinterPeriodStart <= LastRenewalDate)
            {
                NextRenewalDate = WinterPeriodStart.AddMonths(subscription.SubscriptionLengthMonth);
            }

            return NextRenewalDate;
        }
    }
}
